package zzk.activiti;

import org.activiti.bpmn.model.*;
import org.activiti.bpmn.model.Process;
import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/activiti1")
@RestController
public class demo {
    @Resource
    private RuntimeService runtimeService;
    @Resource
    private RepositoryService repositoryService;
    @Resource
    private TaskService taskService;

    /**
     * 删除流程
     */
    @RequestMapping("deleteProcess")
    @ResponseBody
    public void deleteProcess(String id) {
//        String id = "3f0dbef9-b2fd-11ef-a74f-005056c00001";
        //设置true，择优级联删除的效果。false如果已有实例，则会删除错误
        repositoryService.deleteDeployment(id, true);
    }


    /**
     * 部署流程
     */
    @RequestMapping("createProcesses")
    @ResponseBody
    public void createProcesses() {
        //使用获取RepositoryService进行部署
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("static/bpmn/demo1.bpmn")//添加bpmn资源
                .addClasspathResource("static/bpmn/demo1.png")//添加png资源
                .name("员工请假审批流程")
                .deploy();//部署流程
        //输出流程部署的信息
        System.out.println("流程部署id：" + deployment.getId());
        System.out.println("流程部署名称：" + deployment.getName());
    }

    /**
     * 获取流程详情
     */
    @RequestMapping("getBpmnModel")
    @ResponseBody
    public void getBpmnModel() {
        BpmnModel bpmnModel = repositoryService.getBpmnModel("myProcess_1:1:af432db2-b87e-11ef-a854-005056c00001");
        List<Process> processes = bpmnModel.getProcesses();
        Process process=processes.get(0);
        Collection<FlowElement> flowElements=process.getFlowElements();
        for (FlowElement element : flowElements) {
            if (element instanceof UserTask) {
                System.out.println(element.getName());

            }
        }

        System.out.println(111);
        //输出流程部署的信息
//        System.out.println("流程部署id：" + deployment.getId());
//        System.out.println("流程部署名称：" + deployment.getName());
    }








    /**
     * 启动流程（创建新的申请流程实例列表）
     */
    @RequestMapping("addApplication")
    @ResponseBody
    public void addApplication() {
        Map<String, Object> variables = new HashMap<>();
        variables.put("num", 4);
        variables.put("assignee1", "assignee1");
        variables.put("assignee2", "assignee2");
        variables.put("assignee3", "assignee3");
        variables.put("assignee4", "assignee4");

        String key = "myProcess_1";

        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(key, variables);

    }


    /**
     * 处理任务,同时修改流程变量
     */
    @RequestMapping("solveTask1")
    @ResponseBody
    public void solveTask1() {
        String assignee = "assignee1";
        String key = "myProcess_1";
        //根据流程的key和任务负责人 查询任务
        Task task = taskService.createTaskQuery()
                .processDefinitionKey(key)
                .taskAssignee(assignee)
                .singleResult();

        //完成任务，跳到下一个流程
        Map<String, Object> variables = new HashMap<>();
        variables.put("num", 4);
        variables.put("assignee1", "申请人1");
        variables.put("assignee2", "部门经理1");
        variables.put("assignee3", "总经理1");
        variables.put("assignee4", "财务人事1");

        taskService.setVariablesLocal(task.getId(), variables);
        taskService.complete(task.getId());
    }

    /**
     * 处理任务,同时修改流程变量
     */
    @RequestMapping("solveTask2")
    @ResponseBody
    public void solveTask2() {
        String assignee = "部门经理1";
        String key = "myProcess_1";
        //根据流程的key和任务负责人 查询任务
        Task task = taskService.createTaskQuery()
                .processDefinitionKey(key)
                .taskAssignee(assignee)
                .singleResult();

        //完成任务，跳到下一个流程
        Map<String, Object> variables = new HashMap<>();

        variables.put("num", 4);
        variables.put("assignee1", "assignee1");
        variables.put("assignee2", "assignee2");
        variables.put("assignee3", "assignee3");
        variables.put("assignee4", "assignee4");

        taskService.setVariablesLocal(task.getId(), variables);
        taskService.complete(task.getId());
    }

    /**
     * 根据流程的key查询存在哪些任务
     */
    @RequestMapping("searchTaskByKey")
    @ResponseBody
    public void searchTaskByKey() {
        String key = "myProcess_1";
        //根据流程的key和任务负责人 查询任务
        List<Task> tasks = taskService.createTaskQuery()
                .processDefinitionKey(key)
                .list();

        for (Task task : tasks) {
            System.out.println(task.getId());
            System.out.println("流程节点名称："+task.getName());
            System.out.println("流程办理人名称："+task.getAssignee());
            System.out.println("--------------");
        }
    }



}
